import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestExpressApplication } from '@nestjs/platform-express/interfaces/nest-express-application.interface';
import { HttpExceptionFilter } from './core/filter/http-exception/http-exception.filter';
import { TransformInterceptor } from './core/interceptor/transform/transform.interceptor';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common/pipes/validation.pipe';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  // 支持静态资源
  app.useStaticAssets('public', { prefix: '/static' });

  // 注册全局错误的过滤器
  /**
   * @description useGlobalFilters() 方法不为网关或混合应用设置过滤器。
   */
  app.useGlobalFilters(new HttpExceptionFilter());

  // 全局注册拦截器
  app.useGlobalInterceptors(new TransformInterceptor());

  // 设置swagger文档
  const config = new DocumentBuilder()
    .setTitle('Nest-Blog')
    .setDescription('博客后台接口文档')
    .setVersion('0.999.0')
    .addBearerAuth()
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('docs', app, document);

  // 注册全局验证管道
  app.useGlobalPipes(new ValidationPipe());

  await app.listen(process.env.BASE_PORT ?? 3220);
}
bootstrap();
